package com.cyx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class CalcController {

    @RequestMapping("/servlet")
    public void doServlet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Servlet 原生");
    }
    @RequestMapping("/add")
    public String add(int a,int b,Model model){
        int ret = a + b;
        model.addAttribute("msg","result="+ret);
        return "calc";
    }

    @RequestMapping("/mult/{a}/{b}")
    public String mult(@PathVariable int a,@PathVariable int b,Model model){
        model.addAttribute("msg",a*b);
        return "mult";
    }

    @GetMapping("/div/{a}/{b}")
    public String div(@PathVariable int a,@PathVariable int b,Model model){
        model.addAttribute("msg",a*b);
        return "mult";
    }

    @GetMapping("/redirect")
    public String redirect(){
        return "redirect:books";
    }
}
